package per.hx.bs.web.sys.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.Tree;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.constant.Setting;
import per.hx.bs.entity.sys.SysDeptDO;
import per.hx.bs.web.sys.service.SysDeptService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/3/24  22:12
 * 部门控制
 */
@Controller
@RequestMapping(value = "sys/dept")
public class DeptController extends BaseController {

    private final String prefix = "system/dept/";

    private final
    SysDeptService sysDeptService;

    @Autowired
    public DeptController(SysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    @BsCLog("进入部门管理界面")
    @RequiresPermissions("sys:dept:dept")
    @GetMapping(value = {"/", ""})
    public String dept() {
        return prefix + "dept";
    }

    @BsCLog("获取部门列表")
    @RequiresPermissions("sys:dept:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public List<SysDeptDO> list() {
        return sysDeptService.list(new HashMap<String, Object>(0));
    }

    @BsCLog("添加部门信息")
    @RequiresPermissions("sys:dept:add")
    @GetMapping(value = "/add/{pId}")
    public String add(@PathVariable("pId") Long pId, Model model) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", Setting.DEPTNAME);
        } else {
            model.addAttribute("pName", sysDeptService.get(pId).getName());
        }
        return prefix + "add";
    }

    @BsCLog("进入修改部门信息")
    @GetMapping("/edit/{deptId}")
    @RequiresPermissions("sys:dept:edit")
    public String edit(@PathVariable("deptId") Long deptId, Model model) {
        SysDeptDO sysDeptDO = sysDeptService.get(deptId);
        model.addAttribute("sysDept", sysDeptDO);
        if (Setting.DEPT_ROOT_ID.equals(sysDeptDO.getParentId())) {
            model.addAttribute("parentDeptName", "无");
        } else {
            SysDeptDO parDept = sysDeptService.get(sysDeptDO.getParentId());
            model.addAttribute("parentDeptName", parDept.getName());
        }
        return prefix + "edit";
    }

    @BsCLog("添加部门信息")
    @RequiresPermissions("sys:dept:add")
    @PostMapping(value = "/save")
    @ResponseBody
    public ReturnDTO save(SysDeptDO sysDeptDO) {
        sysDeptDO.setCreateId(getUserId());
        return ReturnDTO.check(sysDeptService.save(sysDeptDO));
    }

    @BsCLog("更新部门信息")
    @RequiresPermissions("sys:dept:edit")
    @PostMapping(value = "/update")
    @ResponseBody
    public ReturnDTO update(SysDeptDO sysDeptDO) {
        sysDeptDO.setUpdateId(getUserId());
        return ReturnDTO.check(sysDeptService.update(sysDeptDO));
    }

    @BsCLog("删除部门信息")
    @RequiresPermissions("sys:dept:remove")
    @PostMapping(value = "/remove")
    @ResponseBody
    public ReturnDTO remove(Long deptId) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("parentId", deptId);
        if (sysDeptService.count(map) > 0) {
            return ReturnDTO.error(1, "包含下级部门,不允许修改");
        }
        Map<String, Object> map1 = new HashMap<String, Object>(1);
        map1.put("deptId", deptId);
        if (sysDeptService.countUser(map1) == 0) {
            if (sysDeptService.romove(deptId) > 0) {
                return ReturnDTO.ok();
            }
        } else {
            return ReturnDTO.error("部门包含用户,不允许删除");

        }
        return ReturnDTO.error();
    }

    @BsCLog("批量删除部门信息")
    @RequiresPermissions("sys:dept:remove")
    @PostMapping(value = "/batchRemove")
    @ResponseBody
    public ReturnDTO batchRemove(@RequestParam("ids[]") Long[] deptIds) {
        return ReturnDTO.check(sysDeptService.batchRemove(deptIds));
    }

    @BsCLog("获取树形部门信息")
    @GetMapping(value = "/tree")
    @ResponseBody
    public Tree tree() {
        return JSON.parseObject(sysDeptService.getTree(), Tree.class);
    }

    @BsCLog("进入树形部门信息界面")
    @GetMapping(value = "/treeView")
    public String treeView() {
        return prefix + "deptTree";
    }
}
